import { Injectable, Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { DictItemEntity } from "~/modules/system/dict-item/dict-item.entity";
import { UpdateDictDto } from "./access-dict.dto";
import { AccessDictHouse } from "./access-dict-house.entity";
import { AccessDictRoom } from "./access-dict-room.entity";
import { DictTypeService } from "~/modules/system/dict-type/dict-type.service";

@Injectable()
export class AccessDictService {
	private readonly loggoer = new Logger(AccessDictService.name);
	private readonly defaultDictSelect: any[] = ['id', 'label', 'value', 'remark'];

	constructor(
		private readonly dictTypeService: DictTypeService,
		@InjectRepository(DictItemEntity)
		private dictItemRepository: Repository<DictItemEntity>,
		@InjectRepository(AccessDictHouse)
		private accessDictHouseRepository: Repository<AccessDictHouse>,
		@InjectRepository(AccessDictRoom)
		private accessDictRoomRepository: Repository<AccessDictRoom>,
	) {}

	updateDict(dict: UpdateDictDto): Promise<DictItemEntity> {
		// dict中只有id和value两个字段， 其他字段不允许修改
		return this.dictItemRepository.save(dict);
	}

	/**
	 * 针对字典类型的查询
	 */
	async getDictByType(type: string): Promise<DictItemEntity[]> {
		const types = await this.dictTypeService.getOneByCode(type);
		const dict = await this.dictItemRepository.find({
			select: this.defaultDictSelect,
			where: {type: {id: types.id}}
		});
		return dict;
	}

	/**
	 * 获取房屋类型字典
	 * @returns
	 */
	async getHouseTypeDict(): Promise<DictItemEntity[]> {
		return await this.getDictByType("House")
	}

	/**
	 * 根据朝向和房屋类型获取房屋分析和风水秘诀
	 * @param orient
	 * @param houseTypeId
	 * @returns
	 */
	async getHouseAnalysisAndSecretDict(orient: number, houseTypeId: number): Promise<AccessDictHouse> {
		// 判断 orient 和 houseTypeId 是否为NaN
		if (isNaN(orient) || isNaN(houseTypeId)) {
      throw new Error("数据类型有误")
    }

		return await this.accessDictHouseRepository.findOne({
			where: {
				orientId: orient,
				houseTypeId: houseTypeId,
			}
		});
	}

	/**
	 * 获取房间类型字典
	 * @returns 房间类型字典
	 */
	async getRoomTypeDict(): Promise<DictItemEntity[]> {
		return await this.getDictByType("Room")
	}

	/**
	 * 根据房屋类型、房间类型和房间位置获取房间分析和风水秘诀
	 * @param houseTypeId
	 * @param roomTypeId
	 * @param positionId
	 * @returns
	 */
	async getRoomAnalysisAndSecretDict(houseTypeId: number, roomTypeId: number, positionId: number): Promise<AccessDictRoom> {
		if (isNaN(houseTypeId) || isNaN(roomTypeId) || isNaN(positionId)) {
			throw new Error("数据类型有误")
		}

		return await this.accessDictRoomRepository.findOne({
			where: {
				positionId: positionId,
				houseTypeId: houseTypeId,
				roomTypeId: roomTypeId
			}
		})
	}

	/**
	 * 将字典值转换为标签
	 * @param dictType
	 * @param value
	 * @returns
	 */
	async transferDictValueToLabel(dictType: string, value: string): Promise<string> {
		const dict: DictItemEntity[] = await this.getDictByType(dictType)
		const item: DictItemEntity = dict.find(item => item.value === value)
		return item.label
	}
}